<template>
  <el-container class="home-container">
    <!-- 侧边栏 -->
    <el-aside width="auto">
      <Aside :isCollapse="isCollapse"></Aside>
    </el-aside>
    <el-container>
      <!-- 导航 -->
      <el-header>
        <div class="title">
          <i :class="{'el-icon-s-unfold':isCollapse,'el-icon-s-fold':!isCollapse}" style="margin-right: 5px"
            @click="isCollapse=!isCollapse"></i>
          <span>江苏传智播客科技教育有限公司</span>
        </div>
        <el-dropdown class="avatar">
          <span class="el-dropdown-link">
            <img :src="userInfo.photo" alt="">
            <span>{{userInfo.name}}</span>
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="goSetting">
              <i class="el-icon-setting"></i>
              <span>个人设置</span>
            </el-dropdown-item>
            <el-dropdown-item @click.native="logout">
              <i class="el-icon-unlock"></i>
              <span>退出</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <!-- 主内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
  import globalBus from '@/utils/global-bus.js'
  import Aside from '@/views/Aside'
  import {
    getUseInfo
  } from '@/api/user.js'

  export default {
    name: 'Home',
    components: {
      Aside
    },
    data() {
      return {
        isCollapse: false,
        userInfo: []
      }
    },
    computed: {},
    created() {
      this.loadUseInfo()
      globalBus.$on('update-name', (name) => {
        this.userInfo.name = name
      })
      globalBus.$on('update-photo', (photo) => {
        this.userInfo.photo = photo
      })
    },
    methods: {
      changeCollapse() {
        this.isCollapse = !this.isCollapse
      },
      logout() {
        this.$confirm('是否确定退出?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '退出成功!'
          })
          sessionStorage.clear()
          this.$router.push('/login')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
      },
      loadUseInfo() {
        getUseInfo().then(res => {
          this.userInfo = res.data.data
        })
      },
      goSetting() {
        this.$router.push('/settings')
      }
    }
  }

</script>
<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    color: #333;
    border-bottom: 1px solid #ccc;
    text-align: center;
    line-height: 60px;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dropdown-link {
      display: flex;
      align-items: center;

      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
      }

      span {
        margin-left: 8px;
        margin-right: 8px;
      }
    }
  }

  .el-aside {
    background-color: #002033;
    color: #333;
    line-height: 200px;
    height: 100%;
  }

  .el-main {
    background-color: #fff;
    color: #333;
    padding: 0;
  }

  body>.el-container {
    margin-bottom: 40px;
  }

  .el-container .el-aside {
    line-height: 260px;
  }

</style>
